#pragma once
#include "RBTree.hpp"
namespace xzq1
{
    template <class K, class V>
    class map
    {
    public:
        struct MapKeyOfT
        {
            const K &operator()(const pair<K, V> &kv)
            {
                return kv.first;
            }
        };

    public:
        typedef typename xzq::RBTree<K, pair<K, V>, MapKeyOfT>::iterator iterator;
        iterator begin()
        {
            return _t.begin();
        }
        iterator end()
        {
            return _t.end();
        }
        pair<iterator, bool> Insert(const pair<K, V> &kv)
        {
            return _t.Insert(kv);
        }
        V &operator[](const K &key)
        {
            pair<iterator, bool> ret = _t.Insert(make_pair(key, V()));
            return ret.first->second;
        }

    private:
        xzq::RBTree<K, pair<K, V>, MapKeyOfT> _t;
    };
    void testmap()
    {
        map<int, int> m;
        m.Insert(make_pair(1, 1));
        m.Insert(make_pair(3, 3));
        m.Insert(make_pair(10, 10));
        m.Insert(make_pair(5, 5));
        m.Insert(make_pair(6, 6));
        map<int, int>::iterator it = m.begin();
        while (it != m.end())
        {
            cout << it->first << " : " << it->second << endl;
            ++it;
        }
        cout << endl;
        for (auto kv : m)
        {
            cout << kv.first << " : " << kv.second << endl;
        }
        cout << endl;
        string strs[] = {"西瓜", "樱桃", "西瓜", "苹果", "西瓜", "西瓜", "西瓜", "苹果"};
        map<string, int> countMap;
        for (auto &str : strs)
        {
            countMap[str]++;
        }
        for (auto kv : countMap)
        {
            cout << kv.first << " : " << kv.second << endl;
        }
    }
}